package server;

import java.io.*;

/**
 * @author chenx
 * @date 2021/8/26-21:36
 */
public class Response {
    private OutputStream outputStream;
    public Response(){}

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * 使用输出流输出指定字符串
     * @param content
     * @throws IOException
     */

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * 输出页面
     * @param path
     * @throws IOException
     */
    public void outputHtml(String path) throws IOException {
        // 获取静态资源文件的绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
        // 输入静态资源文件
        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()) {
            // 读取静态资源文件，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else{
            // 输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }
}
